package cqie.cn.frame.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import cqie.cn.frame.entity.SysUser;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-09-10 周五
 * @time 11:50
 * @description
 */
@Component
public class JWTUtils {

    /**
     * 过期时间为一天
     * 正式上线更换为15分钟
     */
    private static final int EXPIRE_TIME = 5;
    //    private static final long EXPIRATIONTIME = 604000000;
    private static final String SIGN = "@#$$%^*()^&wanglin^D#";
    private static final String TOKEN_PREFIX = "Bearer";
    private static final String HEADER_STRING = "Authorization";

    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date getCurrentDate() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * 通过claims获取token中的到期时间
     *
     * @param token
     * @return
     */
    public static Date getExpiration(String token) {
        Claims claims = JWTUtils.getClaim(token);
        if (claims == null) {
            return null;
        } else {
            return claims.getExpiration();
        }
    }

    /**
     * 通过claims获取token中的登录名
     *
     * @param token
     * @return
     */
    public static SysUser getCurrentUser(String token) {
        Claims claims = JWTUtils.getClaim(token);
        if (claims == null) {
            return null;
        } else {
//            //获取当前用户
//            Subject subject = SecurityUtils.getSubject();
//            //封装用户的登录数据
//            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//            user.setPassword();
            return null;
        }
    }

    /**
     * 获取claims
     *
     * @param token
     * @return
     */
    public static Claims getClaim(String token) {
        return Jwts.parser()
                .setSigningKey(SIGN)
                .parseClaimsJws(token)
                .getBody();
    }


    /**
     * @Description: 登录时生成token
     * @Param: [map]
     * @return: java.lang.String
     * @Author: 王林
     * @Date: 2021/9/10
     */
    public static String getToken(Map<String, String> map) {

        Calendar instance = Calendar.getInstance();
//        instance.add(Calendar.SECOND, 20);
        instance.add(Calendar.DATE, EXPIRE_TIME);  // 7天过期

        //创建 jwt builder
        JWTCreator.Builder builder = JWT.create();

        // payload
        map.forEach((k, v) -> {
            builder.withClaim(k, v);
        });

        String token = builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SIGN)); //指定令牌过期时间

        return token;
    }

    /**
     * @Description: token校验
     * @Param: [token]
     * @return: boolean
     * @Author: 王林
     * @Date: 2021/9/10
     */
    public static boolean verify(String token) {
        try {
            JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @Description: 获取token信息
     * @Param: [token]
     * @return: com.auth0.jwt.interfaces.DecodedJWT
     * @Author: 王林
     * @Date: 2021/9/10
     */
    public static DecodedJWT getTokenInfo(String token) {
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
        return verify;
    }


}
